﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HuynhQuangWebsite.Models;
using THS.HtmlHelpers;
using System.IO;
namespace HuynhQuangWebsite.Controllers
{
    public class ManageVideoController : BaseController
    {
        huynhquangco_websiteEntities ctx = new huynhquangco_websiteEntities();
        //
        // GET: /ManageAbout/
        const string virtualPath = "~/Videos/";
        const string physicalPath = "../../../Videos/";
        const int PageSize = 6;
        public ActionResult IndexVideo(int page = 1)
        {
            var model = new HomeAdminListViewModel();
            IList<Video> videos = ctx.Videos.OrderBy(p => p.SortOrder).ToList();
            model.VideoList = videos.Skip((page-1) * PageSize).Take(PageSize).ToList();
            model.VideoInfo = new PagingInfo { CurrentPage = page, ItemsPerPage = PageSize, TotalItems = videos.Count() };
            return View(model);

        }
        public ActionResult Paging(int page = 1)
        {
            var model = new HomeAdminListViewModel();
            IList<Video> Videos = ctx.Videos.ToList();
            model.VideoList = Videos.Skip((page-1) * PageSize).Take(PageSize).ToList();
            model.VideoInfo = new PagingInfo { CurrentPage = page, ItemsPerPage = PageSize, TotalItems = Videos.Count() };
            return PartialView("UCVideoList", model);

        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult UpdateToVideo(int id, FormCollection frm)
        {
            try
            {
                //if (!IsAuthenticateAdmin())
                //    return RedirectToAction("LogOnDashBoard", "DashBoard");

                var about = (from aboutus in ctx.AboutUs where aboutus.AboutUsID == id select aboutus).SingleOrDefault();
                if (about != null)
                {
                    about.AboutUsTitle = frm["AboutUsTitle"];
                    about.AboutUsDetails = frm["AboutUsDetails"];
                    UpdateModel(about);
                    ctx.SaveChanges();
                    return RedirectToAction("IndexAbout");
                }
                else
                {
                    about = new AboutU();
                    about.AboutUsTitle = frm["AboutUsTitle"];
                    about.AboutUsDetails = frm["AboutUsDetails"];
                    ctx.AddToAboutUs(about);
                    ctx.SaveChanges();
                    return RedirectToAction("IndexVideo");
                }
            }
            catch
            {
                return View();
            }
        }

        public ActionResult Detail(int id)
        {
            // TODO: Add update logic here
            // if (!IsAuthenticateAdmin())
            //   return RedirectToAction("LogOn", "AdminHome");
            var video = (from videos in ctx.Videos where videos.VideoID == id select videos).SingleOrDefault();
            if (video != null)
                return View(video);
            else
            {
                video = new Models.Video();
                return View(video);
            }
            //return trang thông báo lỗi
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Detail(int id, HttpPostedFileBase uploadFile)
        {
            try
            {
                // TODO: Add update logic here
                // if (!IsAuthenticateAdmin())
                //   return RedirectToAction("LogOn", "AdminHome");
                Video video = ctx.Videos.Where(p => p.VideoID == id).SingleOrDefault();

                if (uploadFile != null && uploadFile.ContentLength > 0)
                {
                    String oldImage = video.VideoPath;
                    var fileName = Path.GetFileName(uploadFile.FileName);
                    //String sFileName = Utilities.GenerateImageName(Request, virtualPath, fileName, Session["Admin"].ToString());
                    String sFileName = GenerateFileNameByNo(Request, virtualPath, fileName, "V" + DateTime.Now.ToString("ddMMyyyyhhmmss"));
                    string filePath = Path.Combine(Request.MapPath(virtualPath), sFileName);
                    uploadFile.SaveAs(filePath);
                    video.VideoPath = physicalPath + sFileName;
                    //delete image
                    DeleteFile(Request, virtualPath, oldImage);

                }

                UpdateModel(video);
                ctx.SaveChanges();
                ViewData["Msg"] = "Đã cập nhật!";
                return View(video);
                //return RedirectToAction("Indexvideo");
            }
            catch
            {
                return View();
            }
        }

        public ActionResult Delete(int id)
        {
            try
            {
                // TODO: Add delete logic here
                //if (!IsAuthenticateAdmin())
                //    return RedirectToAction("LogOn", "AdminHome");
                Video video = ctx.Videos.Where(p => p.VideoID == id).SingleOrDefault();
                String oldImage = video.VideoPath;
                ctx.Videos.DeleteObject(video);
                ctx.SaveChanges();
                DeleteFile(Request, virtualPath, oldImage);
                return RedirectToAction("IndexVideo");
            }
            catch
            {
                return View();
            }
        }
        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /FunRingManagement/Create

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create([Bind(Exclude = "VideoID")]Video video, HttpPostedFileBase uploadFile)
        {
            // TODO: Add update logic here
            // if (!IsAuthenticateAdmin())
            //   return RedirectToAction("LogOn", "AdminHome");
            try
            {

                if (uploadFile != null && uploadFile.ContentLength > 0)
                {
                    var fileName = Path.GetFileName(uploadFile.FileName);
                    String sFileName = GenerateFileNameByNo(Request, virtualPath, fileName, "V" + DateTime.Now.ToString("ddMMyyyyhhmmss"));
                    string filePath = Path.Combine(Request.MapPath(virtualPath), sFileName);
                    uploadFile.SaveAs(filePath);
                    video.VideoPath = physicalPath + sFileName;

                }
                
                video.AACreateDate = DateTime.Today;
                video.FK_VideoTypeId = Utilities.IDefaultVideoType;
                ctx.AddToVideos(video);
                ctx.SaveChanges();
                ViewData["Msg"] = "Đã tạo thành công!";
                return View();
            }
            catch
            {
                return View();

            }
        }
    }
}
